/**
 * 
 */
package com.carnation.qa.socketserver.proxy.moi;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.carnation.mong.platform.domain.MOIPackage;
import com.carnation.mong.platform.service.PackageService;
import com.carnation.qa.socketserver.domain.Item;
import com.carnation.qa.socketserver.proxy.Builder;

/**
 * @author Nick
 * 
 */
public class PackageServiceImpl extends AbstractMOIServiceImpl {

	private final Log log = LogFactory.getLog(getClass());

	private PackageService service;

	public PackageServiceImpl(List<Item> items) {
		super(items);
		service = ServiceFactory.getService(PackageService.class);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.carnation.qa.socketserver.proxy.AbstractServiceImpl#invoke(com.carnation
	 * .qa.socketserver.domain.Item)
	 */
	@Override
	protected String invoke() {
		Item item = getItems().get(0);
		String method = getMethodName(item);
		String returnStr;
		if ("create".equals(method)) {
			returnStr = create(item);
		} else if ("delete".equals(method)) {
			returnStr = delete(item);
		} else if ("getChildPackages".equals(method)) {
			returnStr = getChildPackages(item);
		} else if ("getPackage".equals(method)) {
			returnStr = getPackage(item);
		} else if ("getPackageByPath".equals(method)) {
			returnStr = getPackageByPath(item);
		} else if ("getParentPackage".equals(method)) {
			returnStr = getParentPackage(item);
		} else if ("getPath".equals(method)) {
			returnStr = getPath(item);
		} else if ("update".equals(method)) {
			returnStr = update(item);
		} else {
			throw new IllegalArgumentException(method);
		}
		return returnStr;
	}

	private String create(Item item) {
		if (item.getChildren().size() == 1) {// 参数个数
			MOIPackage i = instanceMOIPackage(item.getChildren().get(0));
			if (log.isDebugEnabled()) {
				log.debug("create MOIPackage:" + i);
			}
			try {
				Object obj = service.create(i);
				if (log.isDebugEnabled()) {
					log.debug(obj);
				}
				return new Builder(obj, null, item.getTabSeparator())
						.toString();
			} catch (Exception e) {
				if (log.isErrorEnabled()) {
					log.error(item.toString(), e);
				}
				return e.toString();
			}
		}
		throw new IllegalArgumentException(item.toString());
	}

	private String delete(Item item) {
		if (item.getChildren().size() == 1) {// 参数个数
			MOIPackage i = instanceMOIPackage(item.getChildren().get(0));
			if (log.isDebugEnabled()) {
				log.debug("delete MOIPackage:" + i);
			}
			try {
				service.delete(i);
				Object obj = true;
				if (log.isDebugEnabled()) {
					log.debug(obj);
				}
				return new Builder(obj, null, item.getTabSeparator())
						.toString();
			} catch (Exception e) {
				if (log.isErrorEnabled()) {
					log.error(item.toString(), e);
				}
				return e.toString();
			}
		}
		throw new IllegalArgumentException(item.toString());
	}

	private String getChildPackages(Item item) {
		if (item.getChildren().size() == 1) {// 参数个数
			MOIPackage i = instanceMOIPackage(item.getChildren().get(0));
			if (log.isDebugEnabled()) {
				log.debug("getChildPackages p:" + i);
			}
			try {
				Object obj = service.getChildPackages(i);
				if (log.isDebugEnabled()) {
					log.debug(obj);
				}
				return new Builder(obj, null, item.getTabSeparator())
						.toString();
			} catch (Exception e) {
				if (log.isErrorEnabled()) {
					log.error(item.toString(), e);
				}
				return e.toString();
			}
		}
		throw new IllegalArgumentException(item.toString());
	}

	private String getPackage(Item item) {
		if (item.getChildren().size() == 1) {// 参数个数
			String dataId = getString(item.getChildren().get(0).getLabelParts()[1]);
			if (log.isDebugEnabled()) {
				log.debug("getPackage id:" + dataId);
			}
			try {
				Object obj = service.getPackage(dataId);
				if (log.isDebugEnabled()) {
					log.debug(obj);
				}
				return new Builder(obj, null, item.getTabSeparator())
						.toString();
			} catch (Exception e) {
				if (log.isErrorEnabled()) {
					log.error(item.toString(), e);
				}
				return e.toString();
			}
		}
		throw new IllegalArgumentException(item.toString());
	}

	private String getPackageByPath(Item item) {
		if (item.getChildren().size() == 1) {// 参数个数
			String i = getString(item.getChildren().get(0).getLabelParts()[1]);
			if (log.isDebugEnabled()) {
				log.debug("getPackageByPath p:" + i);
			}
			try {
				Object obj = service.getPackageByPath(i);
				if (log.isDebugEnabled()) {
					log.debug(obj);
				}
				return new Builder(obj, null, item.getTabSeparator())
						.toString();
			} catch (Exception e) {
				if (log.isErrorEnabled()) {
					log.error(item.toString(), e);
				}
				return e.toString();
			}
		}
		throw new IllegalArgumentException(item.toString());
	}

	private String getParentPackage(Item item) {
		if (item.getChildren().size() == 1) {// 参数个数
			MOIPackage i = instanceMOIPackage(item.getChildren().get(0));
			if (log.isDebugEnabled()) {
				log.debug("getParentPackage p:" + i);
			}
			try {
				Object obj = service.getParentPackage(i);
				if (log.isDebugEnabled()) {
					log.debug(obj);
				}
				return new Builder(obj, null, item.getTabSeparator())
						.toString();
			} catch (Exception e) {
				if (log.isErrorEnabled()) {
					log.error(item.toString(), e);
				}
				return e.toString();
			}
		}
		throw new IllegalArgumentException(item.toString());
	}

	private String getPath(Item item) {
		if (item.getChildren().size() == 1) {// 参数个数
			MOIPackage i = instanceMOIPackage(item.getChildren().get(0));
			if (log.isDebugEnabled()) {
				log.debug("getPath p:" + i);
			}
			try {
				Object obj = service.getPath(i);
				if (log.isDebugEnabled()) {
					log.debug(obj);
				}
				return new Builder(obj, null, item.getTabSeparator())
						.toString();
			} catch (Exception e) {
				if (log.isErrorEnabled()) {
					log.error(item.toString(), e);
				}
				return e.toString();
			}
		}
		throw new IllegalArgumentException(item.toString());
	}

	private String update(Item item) {
		if (item.getChildren().size() == 1) {// 参数个数
			MOIPackage i = instanceMOIPackage(item.getChildren().get(0));
			if (log.isDebugEnabled()) {
				log.debug("update MOIPackage:" + i);
			}
			try {
				service.update(i);
				Object obj = true;
				if (log.isDebugEnabled()) {
					log.debug(obj);
				}
				return new Builder(obj, null, item.getTabSeparator())
						.toString();
			} catch (Exception e) {
				if (log.isErrorEnabled()) {
					log.error(item.toString(), e);
				}
				return e.toString();
			}
		}
		throw new IllegalArgumentException(item.toString());
	}

}
